#include "progress.h"
//#include "ui_progress.h"

Progress::Progress(QWidget *parent,Qt::WindowFlags f)
    : QDialog(parent,f)
    //, ui(new Ui::Progress)
{
    //ui->setupUi(this);
    setWindowTitle(tr("Progress"));

    numLabel = new QLabel(tr("File Num:"));
    numLineEdit = new QLineEdit;
    numLineEdit->setText(tr("10"));

    typeLabel = new QLabel(tr("Progress Type:"));
    typeComboBox = new QComboBox;
    typeComboBox->addItem(tr("ProgressBar"));
    typeComboBox->addItem(tr("ProgressDialog"));

    progressBar = new QProgressBar;

    startPushButton = new QPushButton(tr("start"));

    QGridLayout *layout = new QGridLayout(this);
    layout->addWidget(numLabel,0,0);
    layout->addWidget(numLineEdit,0,1);
    layout->addWidget(typeLabel,1,0);
    layout->addWidget(typeComboBox,1,1);
    layout->addWidget(progressBar,2,0,1,2);
    layout->addWidget(startPushButton,3,1);

    layout->setMargin(15);
    layout->setSpacing(10);

    connect(startPushButton, SIGNAL(clicked()), this, SLOT(slotStart()));
}

Progress::~Progress()
{
    //delete ui;
}

void Progress::slotStart()
{
    int num = numLineEdit->text().toInt();

    if(typeComboBox->currentIndex() == 0)
    {
        progressBar->setRange(0,num);
        for (int i=1; i<num; i++) {

            progressBar->setValue(i);

            Sleep(1);
        }
    }
    else if(typeComboBox->currentIndex() == 1)
    {
        QProgressDialog *progressDialog = new QProgressDialog(this);

        QFont font("ZYSong18030",12);
        progressDialog->setFont(font);
        progressDialog->setWindowModality(Qt::WindowModal);
        progressDialog->setMinimumDuration(5);
        progressDialog->setWindowTitle(tr("Please Wait"));
        progressDialog->setLabelText(tr("Copying..."));
        progressDialog->setCancelButtonText(tr("Cancel"));
        progressDialog->setRange(0,num);

        for (int i=1;i<num+1;i++) {

            progressDialog->setValue(i);
            qApp->processEvents();

            Sleep(1);
            if(progressDialog->wasCanceled())
                return;
        }
    }
}
